package com.xj.openapi4j.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xj.openapi4j.exception.OpenAPIException;
import com.xj.openapi4j.service.token.YsToken;
import com.xj.openapi4j.util.HttpUtil;
import com.xj.util.Constance;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * Copyright(c) 2015-2015 by yonyouup. All Rights Reserved 计量单位信息
 *
 * @author liujl
 * @version <类版本> , 2015年12月20日
 * @see <相关类/方法>
 * @since <产品/模块版本>
 */
@Service
@Slf4j
public class UnitService {
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    YsToken ysToken;



       public boolean  checkYSUnit(String unitcode,String unitname) throws Exception {
		   Object value = redisTemplate.opsForHash().get(Constance.unit, unitcode);
		   if (value != null) {
               return true;
		   }
		   String token = ysToken.getToken();
           String url = "https://c3.yonyoucloud.com/iuap-api-auth/yonbip/digitalModel/unit/list?access_token=" + token;
            Map<String, Object> paramMap = new HashMap();
            paramMap.put("code", unitcode);
            paramMap.put("pageIndex", 1);
			paramMap.put("pageSize",10);
           String responseStr=   HttpUtil.post(url,JSONObject.toJSONString(paramMap));
		   JSONObject  record = JSONObject.parseObject(responseStr);
           if("200".equals(record.getString("code"))){
              JSONObject data=record.getJSONObject("data");
			  if(data.getJSONArray("recordList").size()>0){
				  redisTemplate.opsForHash().put(Constance.unit,unitcode,unitname);
				  return true;
			  }else {
				return   translateUnit2YS(unitcode,unitname);
			  }
		   }else {
			return   translateUnit2YS(unitcode,unitname);
		   }
	  }

      public  boolean translateUnit2YS(String unitcode,String unitname) throws Exception{
		  String token = ysToken.getToken();
		  String url = "https://c3.yonyoucloud.com/iuap-api-auth/yonbip/digitalModel/batch/unit/save?access_token=" + token;
		  JSONObject data=new JSONObject();
		  JSONObject unitdata=new JSONObject();
		  unitdata.put("stopstatus",0);
		  JSONObject name=new JSONObject();
		  name.put("zh_CN",unitname);
          unitdata.put("name",name);
		  unitdata.put("precision",0);
		  unitdata.put("unitGroup_name","单计量");
		  unitdata.put("_status","Insert");
		  unitdata.put("truncationType",0);
		  unitdata.put("code",unitcode);
		  unitdata.put("bBaseUnit",false);
		  unitdata.put("convertCoefficient",1);
		  unitdata.put("unitGroup",2042050053155586055L);
		  JSONArray jsonArray=new JSONArray();
		  jsonArray.add(unitdata);
          data.put("data",jsonArray);
		  String responseStr=   HttpUtil.post(url,JSONObject.toJSONString(data));
		  JSONObject  record = JSONObject.parseObject(responseStr);
		  if("200".equals(record.getString("code"))){
               JSONObject list=record.getJSONObject("data");
			   if(list.getIntValue("sucessCount")>0){
				   redisTemplate.opsForHash().put(Constance.unit,unitcode,unitname);
				   return true;
			   }
		  }
		  return false;
	  }
}
